package com.cxh.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.common.entity.A_goodsType;
import com.cxh.dao.A_goodsTypeDao;
import com.cxh.dao.impl.A_goodsTypeDaoImpl;

public class A_goodsTypeServlet extends HttpServlet {
	private A_goodsTypeDao gt = null;
	@Override
	public void init() throws ServletException
	{
		gt = new A_goodsTypeDaoImpl();
	}
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String opr = req.getParameter("opr");
		if(sendReply2Update("no_msg", opr, "*信息不完整", req, resp));
		else if(sendReply2Update("not_choice", opr, "未选择操作", req, resp));
		else if(sendReply2Update("add", opr, "*添加操作", req, resp));
		else if(sendReply2Update("ban", opr, "*冻结操作", req, resp));
		else if(sendReply2Update("del", opr, "*删除操作", req, resp));
		if("update".equals(opr)){
			Map<String,Object> mp2 = new HashMap<String, Object>();
			mp2.put("name", req.getParameter("fname"));
			List<A_goodsType> la = gt.findByCondition(mp2, true);
			int super_type_id = la.get(0).getGoodsType_id();
			System.out.println("super_type_id="+super_type_id);
			
			Map<String,Object> mp3 = new HashMap<String, Object>();
			mp3.put("name", req.getParameter("oname"));
			List<A_goodsType> la2 = gt.findByCondition(mp3, true);
			int goodsType_id = la2.get(0).getGoodsType_id();
			int good_level = la2.get(0).getGood_level();
			System.out.println("goodsType_id="+goodsType_id);
			System.out.println("good_level="+good_level);
			
			Map<String,Object> mp = new HashMap<String, Object>();
			mp.put("name",req.getParameter("nname"));
			System.out.println("nname="+req.getParameter("nname"));
			mp.put("super_type_id",super_type_id);
			mp.put("goodsType_id",goodsType_id);
			mp.put("good_level",good_level);
			int rows = gt.updateA_goodsType(mp);
			PrintWriter out = resp.getWriter();
			if(rows>0)
				out.println("<script type='text/javascript'>location.href='goodsTypeServlet';</script>");
			else
				out.println("<script type='text/javascript'>alert('修改失败，原因:+"+0+"');location.href='goodsTypeServlet';</script>");
		}else if("adda".equals(opr)){
			Map<String,Object> mp = new HashMap<String, Object>();
			String name = req.getParameter("nname");
			String fname = req.getParameter("fname");
			mp.put("name",name);
			
			Map<String,Object> mp2 = new HashMap<String, Object>();
			mp2.put("name",fname);
			List<A_goodsType> la = gt.findByCondition(mp2, true);
			int super_type_id = la.get(0).getGoodsType_id();
			int good_level = la.get(0).getGood_level()+1;
			mp.put("super_type_id",super_type_id);
			mp.put("good_level",good_level);
			int rows = gt.addA_goodsType(mp);
			PrintWriter out = resp.getWriter();
			if(rows>0)
				out.println("<script type='text/javascript'>location.href='goodsTypeServlet';</script>");
			else
				out.println("<script type='text/javascript'>alert('添加失败，原因:+"+0+"');location.href='goodsTypeServlet';</script>");
		}else if("delete".equals(opr)){
			Map<String,Object> mp = new HashMap<String, Object>();
			String oname = req.getParameter("oname");
			mp.put("name", oname);
			List<A_goodsType> la = gt.findByCondition(mp, true);
			int goodsType_id = la.get(0).getGoodsType_id();
			int rows = gt.deleteA_goodsType(goodsType_id);
			PrintWriter out = resp.getWriter();
			if(rows>0)
				out.println("<script type='text/javascript'>location.href='goodsTypeServlet';</script>");
			else
				out.println("<script type='text/javascript'>alert('删除失败，原因:+"+0+"');location.href='goodsTypeServlet';</script>");
		}
		else if(sendReply2Update("getlist", req.getParameter("opr2"), "", req, resp));
		else if("toupdate".equals(opr))
			getGoodsTypeTree("GoodsCenter/goods_sort_update.jsp", req, resp);
		else getGoodsTypeTree("GoodsCenter/goods_sort.jsp",req, resp);
	}
	@Override
	public void destroy() {
		super.destroy();
	}
	public void getGoodsTypeTree(String href,HttpServletRequest req,
			HttpServletResponse resp) throws ServletException, IOException {
		List<A_goodsType> lg = gt.findByCondition(null, true);
		req.setAttribute("list_gt", lg);
		req.getRequestDispatcher(href).forward(req, resp);
	}
	private boolean sendReply2Update(String set,String opr,String reply,HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
		if(set.equals(opr)){
			req.setAttribute("reply", reply);
			List<A_goodsType> lg = gt.findByCondition(null, true);
			req.setAttribute("list_gt", lg);
			req.getRequestDispatcher("GoodsCenter/goods_sort_update.jsp").forward(req, resp);
		}
		return set.equals(opr);
	}
}